<?php
namespace app\shop\controller;
use think\Controller;
use think\request;
use think\Db;
use think\Log;
class Taobao extends Controller
{
	//
	public function userstatus()
	{
		$user_id=request()->get("user_id");
		$room=Db::table("room")->where("user_id",$user_id)->find();
		if(empty($room))
		{
			//用户未绑定房间
			return json_encode(["errcode"=>"1","errmsg"=>"用户未绑定房间"]);
		}
		if($room["taobao_user_id"]=="0")
		{
			return json_encode(["errcode"=>"2","errmsg"=>"用户未绑定淘宝账户"]);
		}
		return json_encode(["errcode"=>"0","errmsg"=>""]);

	}
	public function index()
	{
		$user_id=request()->get("user_id");
		$url="https://oauth.taobao.com/authorize?response_type=code&client_id=28044422&redirect_uri=http://www.jfzhiwen.com/shop/taobao/getuser&state=".$user_id."&view=wap";
		header("Location:".$url);
		exit;
	}
	//'code' => 'BJAOTNceO5mO7zATVtOlJ4p49061316',
  	//'state' => '',
	public function getuser()
	{
		$code=request()->get("code");
		$result=$this->gettoken($code);
		echo "<pre>";
		var_dump($result);
		Log::write($result,"taobao");
	}
	public function gettoken($code)
	{
		$url = 'https://oauth.taobao.com/token';
		//  9d2dd0b306127be6faeb3331d7ce336  
		$postfields = [
		    'client_id' => '28044422',
		    'client_secret' => '69d2dd0b306127be6faeb3331d7ce336',
		    'grant_type' => 'authorization_code',
		    'code' => $code,
		    'redirect_uri' => 'http://www.jfzhiwen.com/shop/taobao/getuser',
		    'view' => 'wap'
		];
		$post_data = '';
		foreach ($postfields as $key => $value) {
		    $post_data .= "$key=" . urlencode($value) . "&";
		}
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, substr($post_data, 0, -1));
		$output = curl_exec($ch);
		curl_close($ch);

		$result = json_decode($output, true);
		Log::write($result,"yangzhan");
		return $result;

	}
	public function gettoken1($code)
	{
		//code=OxlukWofLrB1Db1M6aJGF8x2332458&grant_type=authorization_code&client_id=28044422&client_secret=69d2dd0b306127be6faeb3331d7ce336&redirect_uri=http://www.oauth.net/2/ 
		/*$post_data = array(
            "code" => $code,
            "grant_type" => "authorization_code",
            "client_id" => "28044422",	
            "client_secret" =>"69d2dd0b306127be6faeb3331d7ce336",	
            "redirect_uri" =>"http://www.oauth.net/2/",   
        );*/
        $post_data="code=".$code."&grant_type=authorization_code&client_id=28044422&client_secret=69d2dd0b306127be6faeb3331d7ce336&redirect_uri=http://www.oauth.net/2/";
        $url="https://oauth.taobao.com/token";
        $data=$this->curl_post($url,$post_data);
		return $data;
	}

	public function curl_post($url,$post_data){
		$curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        return $data;//json_decode($data, true);
	}
}